package week_eight;

public class ValidPalindrome_125 {
    public boolean isPalindrome(String s) {

        int l = 0, r = s.length() - 1;
        while (l < r) {
            while (l < r && !isDigitAlpha(s.charAt(l))) l++;
            while (l < r && !isDigitAlpha(s.charAt(r))) r--;
            if (l < r) {
                char left = s.charAt(l);
                char right = s.charAt(r);
                if (left >= 'A' && left <= 'Z') left = (char) (left - 'A' + 'a');
                if (right >= 'A' && right <= 'Z') right = (char) (right - 'A' + 'a');
                if (left != right) return false;
                l++;
                r--;
            }
        }
        return true;

    }

    private boolean isDigitAlpha(char c) {
        return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9');
    }
}
